package com.twu.biblioteca;
import java.util.Scanner;

public class UserChoice {

    String msg;
    int optionsCount;
    int choice;
    boolean optionsCountSet = false ;

    public UserChoice(String msg) {

         this.msg = msg;
    }

    public UserChoice() {
        msg = "Choose an option : ";
    }

    public void setOptionsCount(int optionsCount) {
        this.optionsCount = optionsCount;
        optionsCountSet = true ;
    }

    public int getChoice() {
        System.out.println(msg);
        Scanner userInput = new Scanner(System.in);
        choice = userInput.nextInt();
        if( optionsCountSet )
            if(isValidOption())
                 return choice;
            else
            {   msg = "Invalid option!!" + "\n Enter again : ";
                getChoice();
            }
        else
            return choice;

        return -1;

    }

    public boolean isValidOption() {

        if(choice > 0 && choice <=optionsCount )
            return true;
        else
            return false;

    }

}

